package com.movisol.messengerprofileservice.asynctasks;

import java.util.List;

import android.os.AsyncTask;
import android.widget.Toast;

import com.movisol.messengerprofileservice.controllers.ProfileController;
import com.movisol.messengerprofileservice.exceptions.ProfileServiceWebFault;
import com.movisol.messengerprofileservice.helpers.MessengerProfileHelper;
import com.movisol.messengerprofileservice.interfaces.errorcodes.SaveUserProfileErrorCodes;
import com.movisol.tools.HelperUtils;

public class AsyncSubscribePushNotification extends AsyncTask<String, Void, String> {

	private ProfileController	pc	= ProfileController.getInstance();

	@Override
	protected String doInBackground(String... params) {
		String sku = HelperUtils.getConfigParam("SKU", pc.getContext());
		String version = HelperUtils.getApplicationVersion(pc.getContext());
		String languageId = HelperUtils.getDeviceCountry();
		String userId = HelperUtils.getDeviceId(pc.getContext());
		return pc.subscribePushNotification(sku, version, languageId, userId,
				HelperUtils.getConfigParam("ANDROID_MEDIUM", pc.getContext()), params[0]);
	}

	/*
	 * (non-Javadoc)
	 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
	 */
	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		List<Object> list = MessengerProfileHelper.parsePushNotificationResponseToBoolean(result);
		if (list.get(0) != null)
		{
			pc.firePushNotificationSubscribedEvent(null);
		} else
		{
			HelperUtils.LogE(getClass(), SaveUserProfileErrorCodes
					.getErrorByCode(((ProfileServiceWebFault) list.get(1)).getIntegerErrorCode()));
			HelperUtils.showToastMessage(pc.getContext(), SaveUserProfileErrorCodes
					.getErrorByCode(((ProfileServiceWebFault) list.get(1)).getIntegerErrorCode()),
					Toast.LENGTH_LONG);
		}
	}
}
